<?php
/**
 * Created by PhpStorm.
 * User: zyark
 * Date: 2017/8/15
 * Time: 20:48
 */

namespace Admin\Controller;


use Admin\Model\Page;

class ScrollImageController extends BaseController
{
    public function __construct($baseModel)
    {
        parent::__construct(D('ScrollImage'));
        $this->bcItemPush('首页滚动图列表', U('ScrollImage/index'));
    }
    public function index()
    {
        $model = D('ScrollImage');
        //构建查询条件
        $pagesize = 20;
        $search = I('search', '', 'htmlspecialchars');
        $pagenumber = I('pagenum', '');
        if (empty($pagenumber) || $pagenumber <= 0)
            $pagenumber = 1;
        //查询
        $list = $model->getByConditionPage(null, $pagenumber, $pagesize);
        $recordcount = $model->count();
        //封装分页对象
        $page = new Page($pagenumber, $recordcount, $pagesize);
        $page = (array)$page;//转换为数组
        //显示数据
        $this->assign('page', $page);
        $this->assign('search', $search);
        $this->assign('pagenumber', $pagenumber);
        $this->assign('list', $list);
        $this->assign('recordcount', $recordcount);
        $this->display();
    }

    /**
     * 添加滚动图片
     */
    public function addUI()
    {
        $this->bcItemPush('添加滚动图', U('ScrollImage/addUI'));
        $this->display('save');
    }

    /**
     * 编辑
     * @param $id 图片id
     */
    public function editUI($id)
    {
        $map['id'] = ':id';
        $bind[':id'] = $id;
        $imageData = M('ScrollImage')->where($map)->bind($bind)->find();
        $this->assign('imageData', $imageData);
        $this->bcItemPush('编辑滚动图', U('ScrollImage/editUI'));
        $this->display('save');
    }

    public function edit()
    {

        $data['image_url'] = I('post.image_url');
        $data['id'] = I('post.id');
        $data['click_url'] = I('post.click_url');
//        $imageData = M('ScrollImage')->find($data['id']);
        $re = M('ScrollImage')->save($data);
        if ($re) {
//            unlink('./' . $imageData['image_url']);
            $this->ajaxReturn(array('success' => true, 'message' => "上传成功"));
        } else {
//            if ($data['image_url'] != $imageData['image_url']) {
//                unlink('./' . $data['image_url']);
//            }
            $this->ajaxReturn(array('success' => false, 'message' => "上传失败"));
        }
    }

    //添加
    public function add()
    {
        $data['image_url'] = I('post.image_url');
        $re = M('ScrollImage')->add($data);
        if ($re) {
            $this->ajaxReturn(array('success' => true, 'message' => "上传成功"));
        } else {
            unlink('./' . $data['image_url']);
            $this->ajaxReturn(array('success' => false, 'message' => "上传失败"));
        }

    }

    public function delete($id){
        parent::delete($id);
    }

    public function updateField()
    {
        $id = I('id');
        $value = I('value');
        $field = I('field');
        $where['id'] = ':id';
        $bind[':id'] = $id;
        $category = D('ScrollImage')->where($where)->bind($bind)->find();
        $category[$field] = $value;
        $result = D('ScrollImage')->where($where)->bind($bind)->setField($field,$value);
        if($result){
            $this->ajaxReturn(array('success'=>true,'message'=>'修改成功'));
        }else{
            $this->ajaxReturn(array('success'=>false,'message'=>'修改失败'));
        }
    }
}